<template>
  <basic-container>
    <div id="share">
      <h3>分成信息</h3>
      <el-date-picker
        v-model="value"
        type="daterange"
        @change="Datefilter"
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-select
        style="margin-left: 15px"
        v-model="shopnamevalue"
        filterable
        clearable
        placeholder="请选择店铺名称"
        @change="Shopsearch"
      >
        <el-option
          v-for="item in shopname"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        style="margin-left: 10px"
        @change="Shopkeepersearch"
        filterable
        clearable
        placeholder="请选择店主"
        v-model="shopkeepervalue"
      >
        <el-option
          v-for="item in shopkeeper"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        style="margin-left: 10px"
        @change="Splitsearch"
        filterable
        clearable
        placeholder="请选择分成人员"
        v-model="listvalue"
      >
        <el-option
          v-for="item in list"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <avue-crud
        ref="crud"
        :option="option"
        :data="data"
        :page="page"
      ></avue-crud>
    </div>
  </basic-container>
</template>

<script>
import { mapGetters } from "vuex";
// let baseUrl = "http://192.168.0.220:8091";
let baseUrl = "http://122.51.69.142:8091"; //服务器地址
import $ from "jquery";
var dicDatas = {
  face: [], //分类下拉框
  list: [],
  shares: [],
  consumTypeId: [
    {
      label: "销售金额",
      value: 1,
    },
    {
      label: "利润金额",
      value: 2,
    },
    {
      label: "成本",
      value: 3,
    },
  ],
  method: [],
};

export default {
  data() {
    return {
      data: [],
      page: {
        total: 0,
        layout: "total,pager,prev, next",
        background: false,
        pageNum: 1,
        pageSize: 50,
      },
      option: {
        addBtn: false,
        menu: false,
        align: "center",
        menuAlign: "center",
        dialogClickModal:false,
        column: [
          {
            label: "日期",
            prop: "date",
          },
          {
            label: "订单编号",
            prop: "orderId",
          },
          {
            label: "店铺名称",
            prop: "shopName",
          },
          {
            label: "店主",
            prop: "shopkeeperName",
          },
          {
            label: "分成人",
            prop: "partnerName",
          },
          {
            label: "商品名称",
            prop: "goodsName",
          },
          {
            label: "商品类型",
            prop: "categoryName",
          },
          {
            label: "商品售价",
            prop: "price",
          },
          {
            label: "分成类别",
            prop: "scaleCategory",
            type: "select",
            dicData: dicDatas.consumTypeId,
          },
          {
            label: "分成系数",
            prop: "scale",
          },
          {
            label: "分成金额",
            prop: "scalePrice",
          },
        ],
      },
      value: [],
      list: [],
      shopname: [], //店铺名称下拉框
      shopkeeper: [], //店主下拉框
      listvalue: "", //分成人选中数据
      shopnamevalue: "", //店铺名称选中数据
      shopkeepervalue: "", //店主选中数据
      tableSelectList: [], // 选中的数据
      tableLabelList: [], //导出表格的表头
      tablePropList: [], //导出表格的行关联信息
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  mounted() {
    this.shop();
    this.keeper();
    this.getDataList();
  },
  methods: {
    // 进入页面加载
    getDataList() {
      let _this = this;
      _this.$ajax
        .post(`${baseUrl}/pc/getScaleDetailList`, {
          pageNum: _this.page.pageNum,
          pageSize: _this.page.pageSize,
        })
        .then(function (res) {
          //请求成功
          let resData = res.data;
          if (resData.code == 200) {
            _this.page.total = resData.data.count;
            _this.data = resData.data.scaleDetailList;
          } else {
            _this.$message.error(resData.msg);
          }
        })
        .catch(function (error) {
          //请求失败
          console.log(error);
        });
    },
    // 获取店铺名称
    shop() {
      this.userInfo.userInfo.roles[0].shops.forEach((item) => {
        this.shopname.push({
          value: item.shopId,
          label: item.name,
        });
      });
      // let _this = this;
      // _this.$ajax
      //   .post(`${baseUrl}/pc/getShopList`)
      //   .then(function (res) {
      //     //请求成功
      //     let resData = res.data;
      //     if (resData.code == 200) {
      //       resData.data.forEach((item) => {
      //         _this.shopname.push({
      //           value: item.shopId,
      //           label: item.name,
      //         });
      //       });
      //     } else {
      //       _this.$message.error(resData.msg);
      //     }
      //   })
      //   .catch(function (error) {
      //     //请求失败
      //     console.log(error);
      //   });
    },
    // 获取店主、分成人列表
    keeper() {
      let _this = this;
      _this.$ajax
        .post(`${baseUrl}/pc/getShopkeeperList`)
        .then(function (res) {
          //请求成功
          let resData = res.data;
          if (resData.code == 200) {
            resData.data.forEach((item) => {
              _this.shopkeeper.push({
                label: item.name,
                value: item.id + "",
              });
            });
          } else {
            _this.$message.error(resData.msg);
          }
        })
        .catch(function (error) {
          //请求失败
          console.log(error);
        });
      // 获取分成人员列表
      if (_this.list.length == 0) {
        _this.$ajax
          .post(`${baseUrl}/pc/getBindablePartnerList`)
          .then(function (res) {
            //请求成功
            _this.list = [];
            let resData = res.data;
            if (resData.code == 200) {
              resData.data.forEach((item) => {
                _this.list.push({
                  value: item.id,
                  label: item.name,
                });
              });
            } else {
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      }
    },
    rowCell(row, index) {
      this.$refs.crud.rowCell(row, index);
    },
    // 新增
    rowSave(form, done) {
      this.$message.success("新增数据" + JSON.stringify(form));
      done();
    },
    // 修改
    rowUpdate(form, index, done) {
      this.$message.success(
        "编辑数据" + JSON.stringify(form) + "数据序号" + index
      );
      done();
    },
    handleRowClick(row) {
      this.$message({
        showClose: true,
        message: "序号" + row.$index,
        type: "success",
      });
    },
    //导出按钮
    openSelExcel(st) {
      let _this = this;
      if (st == 1) {
        if (_this.tableSelectList.length === 0) {
          _this.$message({
            message: "请至少选择一条需要导出的数据",
            type: "warning",
          });
          return;
        }
        _this
          .$confirm("该操作将导出选中的数据，是否继续", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            //导出
            let excelList = _this.tableSelectList;
            console.log(excelList);
            _this.openExcel(excelList);
          })
          .catch(() => {});
      } else {
        _this
          .$confirm("该操作将导出表格中的数据，是否继续", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            //导出

            let excelList = _this.data;
            console.log(excelList);
            _this.openExcel(excelList);
          })
          .catch(() => {});
      }
    },
    // 日期选择
    Datefilter() {
      let _this = this;
      if (
        _this.value == null &&
        _this.shopnamevalue == "" &&
        _this.shopkeepervalue == "" &&
        _this.listvalue == ""
      ) {
        _this.getDataList();
      } else if (
        _this.shopnamevalue == "" &&
        _this.shopkeepervalue == "" &&
        _this.listvalue == ""
      ) {
        console.log(_this.value[0], typeof _this.value[0]);
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            startTime: _this.value[0],
            endTime: _this.value[1],
          })
          .then(function (res) {
            //请求成功
            console.log(res.data);
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.shopkeepervalue == "" && _this.listvalue == "") {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            startTime: _this.value[0],
            endTime: _this.value[1],
            shopId: _this.shopnamevalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.listvalue == "") {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            startTime: _this.value[0],
            endTime: _this.value[1],
            shopId: _this.shopnamevalue,
            shopkeeperId: _this.shopkeepervalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            startTime: _this.value[0],
            endTime: _this.value[1],
            shopId: _this.shopnamevalue,
            shopkeeperId: _this.shopkeepervalue,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      }
    },
    // 店铺名称搜索框
    Shopsearch() {
      let _this = this;
      if (
        _this.value.length == 0 &&
        _this.shopkeepervalue == "" &&
        _this.listvalue == ""
      ) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopId: _this.shopnamevalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.shopkeepervalue == "" && _this.value.length == 0) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopId: _this.shopnamevalue,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.value.length == 0) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopId: _this.shopnamevalue,
            partnerId: _this.listvalue,
            shopkeeperId: _this.shopkeepervalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            startTime: _this.value[0],
            endTime: _this.value[1],
            shopId: _this.shopnamevalue,
            shopkeeperId: _this.shopkeepervalue,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      }
    },
    // 店主搜索框
    Shopkeepersearch() {
      let _this = this;
      if (
        _this.value.length == 0 &&
        _this.shopnamevalue == "" &&
        _this.listvalue == ""
      ) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopkeeperId: _this.shopkeepervalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.shopnamevalue == "" && _this.value.length == 0) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopkeeperId: _this.shopkeepervalue,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.value.length == 0) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopId: _this.shopnamevalue,
            partnerId: _this.listvalue,
            shopkeeperId: _this.shopkeepervalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            startTime: _this.value[0],
            endTime: _this.value[1],
            shopId: _this.shopnamevalue,
            shopkeeperId: _this.shopkeepervalue,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      }
    },
    // 分成人搜索
    Splitsearch() {
      let _this = this;
      if (
        _this.value.length == 0 &&
        _this.shopnamevalue == "" &&
        _this.shopkeepervalue == ""
      ) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.shopnamevalue == "" && _this.value.length == 0) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopkeeperId: _this.shopkeepervalue,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else if (_this.value.length == 0) {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            shopId: _this.shopnamevalue,
            partnerId: _this.listvalue,
            shopkeeperId: _this.shopkeepervalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      } else {
        _this.$ajax
          .post(`${baseUrl}/pc/getScaleDetailList`, {
            pageNum: _this.page.pageNum,
            pageSize: _this.page.pageSize,
            startTime: _this.value[0],
            endTime: _this.value[1],
            shopId: _this.shopnamevalue,
            shopkeeperId: _this.shopkeepervalue,
            partnerId: _this.listvalue,
          })
          .then(function (res) {
            //请求成功
            let resData = res.data;
            if (resData.code == 200) {
              $(".el-pagination").css("display", "inline-block");
              _this.page.total = resData.data.count;
              _this.data = resData.data.scaleDetailList;
            } else {
              _this.data = [];
              $(".el-pagination").css("display", "none");
              _this.$message.error(resData.msg);
            }
          })
          .catch(function (error) {
            //请求失败
            console.log(error);
          });
      }
    },
  },
};
</script>

<style>
#share h3 {
  margin: 20px 0;
}
</style>